<template>
  <el-form
    ref="formRef"
    :model="form"
    :rules="rules"
    label-width="80"
    label-position="top"
  >
    <el-row class="w-full" :gutter="10">
      <el-col :span="12">
        <el-form-item label="优化要求">
          <el-input
            v-model="form.optimization_requirement"
            placeholder="若无特殊要求，则由AI 自主优化"
          />
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="优化内容" prop="optimize_content">
          <el-input
            v-model="form.optimize_content"
            placeholder="请输入"
            :rows="16"
            type="textarea"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script name="SyntaxOptimization" setup>
import { ElMessage, ElMessageBox } from "element-plus";

const data = reactive({
  form: {},
  rules: {
    optimize_content: [{ required: true, message: "请输入优化内容" }],
  },
});

const { form, rules } = toRefs(data);

const formRef = ref();
const emit = defineEmits(["submit"]);
const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      const userText = getFormData();
      emit("submit", {
        type: "V3",
        userText,
      });
    }
  });
};

const getFormData = () => {
  const preamble = `请根据待优化内容${
    form.value.optimization_requirement ? "和优化要求" : ""
  }，对语法结构进行优化，并按照输出结构输出\n`;

  let preambleContentTypes = [];
  if (form.value.optimize_content) {
    preambleContentTypes.push(`待优化内容：${form.value.optimize_content}\n`);
  }

  if (form.value.optimization_requirement) {
    preambleContentTypes.push(`优化要求：${form.value.optimization_requirement}`);
  }

  const preambleWithContentTypes = preamble + preambleContentTypes.join("") + "；\n";
  const dataParts = [preambleWithContentTypes];

  // 相关信息及要求
  const relevantInfoParts = [];
  let index = 1; // 初始化序号

  if (form.value.optimize_content) {
    relevantInfoParts.push(`${index++}、优化后内容`);
    relevantInfoParts.push(`${index++}、优化后内容翻译成中文`);
    relevantInfoParts.push(`${index++}、优化理由`);
  }

  if (relevantInfoParts.length > 0) {
    dataParts.push("输出结构: ");
    dataParts.push(relevantInfoParts.join("\n"));
  }
  // 最终拼接成完整的字符串
  const finalData = dataParts.join("\n");

  return finalData; // 如果有需要返回数据的地方
};

const reset = () => {
  if (!formRef.value) return;
  formRef.value.resetFields();
  init();
};

const init = () => {
  form.value = {
    language_id: 2,
    language_name: "英文",
  };
};

onMounted(() => {
  init();
});

defineExpose({ submit, reset });
</script>
<style lang="scss" scoped></style>
